/*********************************************************************************
  *Copyright (c)    2021   xldeng
  *FileName:        341b.cpp.c
  *Author:          xldeng
  *Version:         1.0
  *Date:            2021/2/16 10:45
  *Description:     
  *Others:          
  *Function List:   
     1.…………
  *History:  
     1.Date:
       Author:
       Modification:
**********************************************************************************/
#include "../header.h"

class NestedInteger {
public:
    // Return true if this NestedInteger holds a single integer, rather than a nested list.
    bool isInteger() const;

    // Return the single integer that this NestedInteger holds, if it holds a single integer
    // The result is undefined if this NestedInteger holds a nested list
    int getInteger() const;

    // Return the nested list that this NestedInteger holds, if it holds a nested list
    // The result is undefined if this NestedInteger holds a single integer
    const vector<NestedInteger> &getList() const;
};

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (int i = 0; i < nestedList.size(); i++) {
            NestedInteger *child = new NestedInteger(nestedList[i]);
            list.push_back(child);
        }
    }

    int next() {
        int ans = list.front()->getInteger();
        delete list.front();
        list.pop_front();
        return ans;
    }

    bool hasNext() {
        while (!list.empty() && !list.front()->isInteger()) {
            vector<NestedInteger> first = list.front()->getList();
            delete list.front();
            list.pop_front();
            for (int i = first.size() - 1; i >= 0; i--) {
                NestedInteger *child = new NestedInteger(first[i]);
                list.emplace_front(child);
            }
        }
        return !list.empty();
    }

private:
    list<NestedInteger *> list;
};
